<?php
if(!isset($getResultAsArray)){
    $getResultAsArray = false;
}

extract(extractGet(), EXTR_PREFIX_ALL, 'b');
if($getResultAsArray){
    $b_user_id = $loginUserID;
}
if(isset($b_user_id)){
    $user = R::load('user', $b_user_id);
    if($user->id == 0){
        jsonErrorDie("User with provided id = " . $b_user_id . " does not exist.");
    }

    //get groups where user is developer kanban master or product owner
    $groups = [];
    foreach(R::findAll('group') as $group)
    {
        if((isInBeans($user->id, $group->sharedUserList) && in_array($user->id, json_decode($group->developers)))
    || $group->kanbanMaster == $user->id || $group->productOwner == $user->id){
            $groups[] = $group->id;
        }
    }
    if (count($groups) == 0){
        jsonSuccessDie('boards', '[]');
    }

    $boards = R::findAll('board', 'group_id IN (' . implode(',', $groups) . ')');

    if($getResultAsArray){
        $boards = R::exportAll($boards, false, 'id,name,wip');
    }
    else{
        jsonSuccessDie("boards", R::exportAll($boards, false, 'id,name,wip'));
    }

}
else{
jsonErrorDie("Not all mandatory parameters set!");
}
